package Cani;
import java.awt.Color;
import java.util.Stack;

import Desen.HighlightDesen;
import GUI.SimulareDesenPanel;

public class InLatime {
	U1 u1 = new U1();
	U2 u2 = new U2();
	G1 g1 = new G1();
	G2 g2 = new G2();
	T12 t12 = new T12();
	T21 t21 = new T21();

	Operator op[] = { u1, u2, g1, g2, t12, t21 };
	
	Stack<Nod> nod_solutie = new Stack<Nod>();

	public InLatime(){
		
	}
	public void adaugare_prim_nod(Nod n) {
		nod_solutie.push(n);
	}

	public Stack<Nod> getNod_solutie() {
		return nod_solutie;
	}

	public void setNod_solutie(Stack<Nod> nod_solutie) {
		this.nod_solutie = nod_solutie;
	}

	public void rezolvare(Nod n, int index_stiva) {
		Nod clona = new Nod(n);
		if (solutie(n)) {
			return;
		} else {
			//System.out.println("Din " + clona + " s-a expandat:");
			SimulareDesenPanel.textdesen.append("Din " + clona + " s-a expandat:\n");
			for (int i = 0; i < 6; i++) {
				if (op[i].condition(clona)) {
					SimulareDesenPanel.textdesen.append("\t\t" + op[i].getClass());
					//System.out.print("\t\t\t\t" + op[i].getClass());
					if (cauta_in_stiva_nod_solutie(op[i].action(clona)) == false) {
						Nod nod_expandat = new Nod(op[i].action(clona));
						SimulareDesenPanel.textdesen.append("--->" + nod_expandat+"\n");
						//System.out.println("--->" + nod_expandat);
						if (solutie(nod_expandat)){
							HighlightDesen.drawHighlight(op[i].action(clona)+"",15,Color.green);
							return;
						}
						nod_solutie.push(nod_expandat);
					} else{
						SimulareDesenPanel.textdesen.append("--->" + op[i].action(clona)+ " (use)\n");
						HighlightDesen.drawHighlight(op[i].action(clona)+" (use)",0,Color.red);
					}
				}
			}
			index_stiva++;
			rezolvare(nod_solutie.get(index_stiva), index_stiva++);
		}
	}

	public boolean cauta_in_stiva_nod_solutie(Nod n) {
		for (int i = 0; i < nod_solutie.size(); i++)
			if ((nod_solutie.get(i).x == n.x) && (nod_solutie.get(i).y == n.y))
				return true;
		return false;
	}

	public boolean solutie(Nod n) {
		if (n.getY() == 2)
			return true;
		return false;

	}

}
